package com.example.qxfw.qxxx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.qx.Doppler;
import com.example.qxfw.mongodb.qx.dto.LdFilePngDto;
import com.example.qxfw.mongodb.qx.entity.QxLdPngFile;
import com.example.qxfw.mongodb.qx.service.QxLdPngFileService;
import com.example.qxfw.qxxx.service.DopplerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.qxfw.common.util.DateUtil.convertDateFormat;

@Slf4j
@Api(tags = "气象信息-雷达图")
@RestController
@CrossOrigin
@RequestMapping("/RadarChartController")
public class RadarChartController {

    @Autowired
    private DopplerService dopplerService;

    @Autowired
    private QxLdPngFileService qxLdPngFileService;


    @ApiOperation("气象信息-雷达图-获取雷达最新时间")
    @GetMapping("/getMaxTime")
    public SysResult getMaxTime() {
        List<Doppler> maxTime = dopplerService.getMaxTime();
        return SysResult.success(maxTime);
    }


    @ApiOperation("气象信息-雷达图-获取dropper表雷达图列表")
    @GetMapping("/selectDoppler")
    public SysResult selectDoppler(String dateCharStart, String dateCharEnd, String ldType, float elevation) {
        List<Doppler> dopplerList = dopplerService.selectDopplerByDateCharLdTypeAndElevation(dateCharStart, dateCharEnd, ldType, elevation);
        return SysResult.success(dopplerList);
    }

    @ApiOperation("气象信息-雷达图-获取dropper表雷达图列表")
    @GetMapping("/selectDopplerList")
    public SysResult selectDopplerList(String dateCharStart, String dateCharEnd, String ldType, float elevations) {
        List<LdFilePngDto> ldFilePngDtoList = new ArrayList<>();
        List<Doppler> dopplerList = dopplerService.selectDopplerByDateCharLdTypeAndElevation(dateCharStart, dateCharEnd, ldType, elevations);
        List<QxLdPngFile> qxLdPngFileList = new ArrayList<>();
        dopplerList.forEach(dop -> {
            String ldtype = dop.getLdtype();
            String elevation = dop.getElevation().toString();
            String stationid = dop.getStationid();
            String timechar = dop.getTimechar();
            String datechar = dop.getDatechar().toString();
            String dateTime = convertDateFormat(datechar);
            assert dateTime != null;
            String[] parts = dateTime.split("-");
            String dateyear = parts[0];
            List<QxLdPngFile> selectIdDateCharTimeChar = qxLdPngFileService.findByDateCharAndTimeCharAndStationIdAndElevationAndLdTypeAndDateYear(dateTime, timechar, stationid, elevation, ldtype, dateyear);

            qxLdPngFileList.addAll(selectIdDateCharTimeChar);

        });

        List<QxLdPngFile> qxLdPngFiles = qxLdPngFileList.stream().distinct().collect(Collectors.toList());
        qxLdPngFiles.forEach(q -> {
            String q_id = q.get_id();
            String datechar = q.getDatechar();
            String timechar = q.getTimechar();
            String hours = timechar.substring(0, 2);
            String minutes = timechar.substring(2);
            LdFilePngDto ldFilePngDto = new LdFilePngDto();
            ldFilePngDto.setId(q_id);
            ldFilePngDto.setName(datechar + " " + hours + ":" + minutes + ":00");
            ldFilePngDtoList.add(ldFilePngDto);
        });
        return SysResult.success(ldFilePngDtoList);
    }

    @ApiOperation("气象信息-雷达图-根据id获取雷达图")
    @GetMapping("/getDopplerById")
    public ResponseEntity<byte[]> getDopplerById(String id, String name) {
        QxLdPngFile qxLdPngFile = qxLdPngFileService.selectById(id);
        byte[] data = qxLdPngFile.getData();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG); // 设置为图片的MIME类型
        headers.setContentDisposition(ContentDisposition.builder("attachment").filename(name + ".png").build());
        return new ResponseEntity<>(data, headers, HttpStatus.OK);
    }

}
